<template>
	<view class="order-wrapper" :style="{paddingTop: paddingTop + 'px'}">
		<navBar title="订单列表" :customBack="true" @back="handleBack" @home="handleHome" />
		<view class="order-tab" :style="{top: paddingTop + 'px'}">
			<u-tabs :list="list" :scrollable="false" :current="current" lineColor="#CF1A1B" lineWidth="55"
				lineHeight="4" :activeStyle="{
	  			color: '#CF1A1B',
				'font-size':'14px'
	  		}" :inactiveStyle="{
	  			color: 'rgba(0,0,0,0.65)',
				'font-size':'14px'
	  		}" itemStyle="height: 44px;" @change="handleTab">
			</u-tabs>
		</view>
		<view v-if="orderList.length == 0" class="empty">
			<image src="@/static/empty.png"></image>
			<text>无数据</text>
		</view>
		<view class="list-wrapper">
			<view class="order-list" v-for="item in orderList">
				<view class="order-item" @click.stop="onDetail(item)">
					<view class="order-item-h">
						<view class="store-name">
							<image class="img" src="../../static/img/tabbar/ic_store.png"></image>
							<text class="name">{{ item.terminalName }}</text>
						</view>
						<view class="order-status">
							<!-- 车队用户登录，订单状态则是待审核，其它用户是待付款 -->
							<text v-if="item.orderState == 1" style="color:#F68B53"> {{ getUserInfo.fleetFlag ? '待审核' : '待付款'}} </text>
							<text v-if="item.orderState == 4" style="color:#1E6CFF">待使用</text>
							<text v-if="item.orderState == 5" style="color:#F55932">服务中</text>
							<text v-if="item.orderState == 6" style="color:#1EC48A">已完成</text>
							<text v-if="item.orderState == -1|| item.orderState == 8" style="color:#787878">已取消</text>
							<text v-if="item.orderState == 7" style="color:#1E6CFF">待使用-退款中</text>
						</view>
					</view>
					<view class="order-item-c">
						<view class="goods-list">
							<view class="goods-item" v-for="(list,index) in item.orderItems">
								<view class="goods-img">
									<image class="img"
										:src="list.image?list.image:'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/order/goods_default_square.png'">
									</image>
								</view>
								<view class="goods-info"
									:class="{'end-goods-info':index == (item.orderItems.length - 1) }">
									<view class="info">
										<view class="goods-name">{{ list.goodsName }}</view>
										<!-- <view class="goods-pid">{{ list.propertiesDesc }}</view> -->
										<view class="goods-price">
											<view class="price"> <text
													style="font-size: 12px;">￥</text>{{ list.sellPrice.toFixed(2) }}
											</view>
											<view class="num">×{{ list.quantity }}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="goods-total">共 {{ item.orderItems.length }} 件商品 应付金额：<text class="total-price"> <text
								style="font-size: 12px;">￥</text>{{ item.actuallyPrice.toFixed(2)}}</text></view>
					<view class="order-item-b">
						<!-- 已完成状态的 -->
						<!-- <u-button type="primary" text="去评价" size="small"
							v-if="item.showEvaluate && item.orderState == 6"
							@click.native.stop="goEval(item)"></u-button> -->
						<u-button type="primary" text="再次购买" :plain="true" size="small"
							v-if="item.orderState == -1||item.orderState == 6||item.orderState == 5||item.orderState == 8"
							@click.native.stop="goGoodsDetail(item.orderItems[0].goodsCode)"></u-button>
						<u-button type="primary" text="退款进度" :plain="true" size="small" v-if="item.orderState == 8"
							@click.native.stop="afterSaleDetail(item)">
						</u-button>
						<!-- 车队用户登录，订单状态则是待审核，其它用户是待付款，待审核状态不能操作 -->
						<u-button type="primary" text="去支付" size="small" v-if="item.orderState == 1 && !getUserInfo.fleetFlag"
							@click.native.stop="onPay(item)"></u-button>
						<u-button type="primary" text="去门店" size="small"
							v-if="item.orderState == 4||item.orderState == 7" @click.native.stop="handleDH(item)">
						</u-button>
						<u-button type="primary" text="分享核销码" :plain="true" size="small"
							v-if="item.orderState == 4||item.orderState == 7" @click.native.stop="onShare(item)">
						</u-button>
						<u-button type="primary" text="查看发票" :plain="true" size="small"  @click.native.stop="toInvoiceDetails(item)" 
							v-if="(item.orderState == 4||item.orderState == 6||item.orderState == 5||item.orderState == 7)&&item.invoiceId"></u-button>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" v-if="loadVisible" :contentText="contentText" />
		<!-- 分享组件 -->
		<share :isOpen="show" @cancel="show=false" @onShareTimeline="onShareTimeline" :orderNo="currentOrder.orderNo">
		</share>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex'
	import navBar from '@/components/navBar.vue'
	import share from '@/components/share.vue'
	import payment from '@/mixins/payment.js'

	export default {
		components: {
			navBar,
			share
		},
		mixins: [payment],
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				current: 0,
				list: [{
						name: '全部',
						status: ''
					}, {
						name: '待付款',
						status: 1,
					}, {
						name: '待使用',
						status: 4
					},
					{
						name: '服务中',
						status: 5
					},
					{
						name: '已完成',
						status: 6
					},
					{
						name: '已取消',
						status: -1
					}
				],
				orderState: '',
				pageNum: 1,
				pageSize: 10,
				orderList: [],
				total: 0,
				emptyVisible: false,
				status: 'more',
				loadVisible: false,
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了",
				},
				orderStates: null,
				paddingTop: '',
				show: false,
				currentOrder: ''
			}
		},
		computed: {
			...mapGetters('user', ['getUserInfo']),
		},
		methods: {
			goEval(item){
				uni.navigateTo({
					url: `/orderPack/evaluate/actionEvaluate?id=${item.id}&storeName=${item.terminalInfo.name}&storeImage=${item.terminalInfo.photo1}&source=orderList`,
				})
			},
			handleBack() {
				uni.switchTab({
					url: '/pages/mine/home'
				})
			},
			handleHome() {
				uni.switchTab({
					url: '/pages/index/index/index'
				})
			},
			handleTab(e) {
				this.orderState = e.status
				this.pageNum = 1
				this.orderList = []
				this.current = e.index
				this.orderStates = null
				if (e.status == -1) {
					this.orderState = ''
					this.orderStates = [-1, 8]
				}
				if (e.status == 4) {
					this.orderState = ''
					this.orderStates = [4, 7]
				}
				this.initData()
			},
			//获取数据
			async initData() {
				try {
					const res = await this.$request({
						method: 'POST',
						url: '/order-center/car-owner-order/getOrderByUserId',
						data: {
							loginUserId: this.getUserInfo.id,
							orderState: this.orderState,
							pageNum: this.pageNum,
							pageSize: this.pageSize,
							orderStates: this.orderStates
						}
					})
					this.orderList = this.orderList.concat(res.data)
					this.total = res.count
					this.loadVisible = this.total > this.pageSize
					this.emptyVisible = this.total === 0
					this.status = 'more'
					uni.hideLoading()
				} catch (res) {
					uni.hideLoading()
					uni.showToast({
						title: '请求失败',
						icon: 'error'
					})
				}

				uni.stopPullDownRefresh();
			},
			//再次购买进入商品详情页面
			goGoodsDetail(code) {
				uni.navigateTo({
					url: `/components/goodsDetail?jfcode=${code}`
				});
			},
			//查看详情
			onDetail(item) {
				// 立即购买
				uni.navigateTo({
					url: '/orderPack/detail/index',
					success: (res) => {
						res.eventChannel.emit('detailInfo', {
							data: item
						})
					}
				})
			},
			//去支付
			onPay(val) {
				const {
					id,
					counterUrl
				} = val
				this.orderId = id
				this.toPay(counterUrl)
			},
			//去门店
			handleDH(val) {
				const {
					lng,
					lat,
					name,
					address
				} = val.terminalInfo
				uni.openLocation({
					longitude: lng,
					latitude: lat,
					name,
					address,
				});
			},
			//分享
			onShare(val) {
				this.currentOrder = val
				this.show = true
			},
			//查看退款进度
			afterSaleDetail(item) {
				uni.navigateTo({
					url: `/orderPack/aftermarket/refundProgress?id=${item.refundOrderId}`,
				})
			},
			// 跳转发票详情页
			toInvoiceDetails(item) {
			  uni.navigateTo({
			    url: '/minePack/function/invoice/invoiceDetails',
			    success: (res) => {
			      res.eventChannel.emit('invoiceDetails',{invoiceId:item.invoiceId,orderId: item.id})
			    }
			  })
			},

		},

		onReachBottom() {
			if (this.orderList.length >= this.total) {
				this.status = 'noMore'
				return
			}
			this.loadVisible = true
			this.status = 'loading'
			this.pageNum++
			this.initData()
		},
		onPullDownRefresh() {
			this.loadVisible = false
			this.pageNum = 1
			this.orderList = []
			this.initData()
		},
		onShow(){
			// console.log(123);
			this.orderList = []
			this.initData()
		},
		onLoad(option) {
			this.paddingTop = this.$store.getters['global/getSystemInfo'].navBarHeight
			wx.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline']
			})
			uni.showLoading({
				title: '加载中...'
			})
			//参数status => 订单状态：1:待付款; 2:待发货; 3:待收货;4:待使用; 5:服务中;6:已完成;
			if (option.status) {
				this.orderState = option.status
				switch (option.status) {
					case '1':
						this.current = 1
						break;
					case '4':
						this.orderState = ''
						this.orderStates = [4, 7]
						this.current = 2
						break;
					case '5':
						this.current = 3
						break;
					case '6':
						this.current = 4
						break;
					case '-1':
						this.orderState = ''
						this.orderStates = [-1, 8]
						this.current = 5
						break;
					default:
						break;
				};
			} else {
				this.current = 0
				this.orderState = null
				this.orderStates = null
			}
			
			// 车队用户登录，订单状态则是待审核，其它用户是待付款
			if(this.getUserInfo.fleetFlag){
				this.list[1].name = '待审核'
			}
		},
		// 分享到聊天
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				return {
					title: '卡友之家 换胎服务',
					desc: '请主动告知门店您购买的服务项目后验码使用!',
					path: `/pages/webview/webview?type=shareNo&orderNo=${this.currentOrder.orderNo}`,
					imageUrl: this.currentOrder.terminalInfo.photo1 ? this.currentOrder.terminalInfo.photo1 :
						'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/mine/user_avatar.png'
				}
			}
			return {
				title: '阿特拉斯核销码',
				path: '/pages/index',
				imageUrl: 'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/mine/user_avatar.png'
			}
		},

		// onShareTimeline() {
		// 	return {
		// 		title:'租车' , // 标题
		// 		path: 'www.baidu.com',
		// 		query:{
		// 	     	id:'234324',
		// 		 	name:'王串串',
		// 		 	age:'57'
		// 		},
		// 		imageUrl: 'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/mine/user_avatar.png' // 分享图
		// 	};
		// }
	}
</script>

<style lang="scss" scoped>
	.order-wrapper {
		width: 100%;
		height: 100%;
		font-size: 14px;
		box-sizing: border-box;
		padding-bottom: env(safe-area-inset-bottom);

		/deep/ .u-tabs__wrapper__nav__line {
			margin-left: 16px;
		}

		/deep/ .u-tabs__wrapper__nav__item {
			padding: 0;
		}

		.empty {
			width: 100%;
			height: 80%;
			position: absolute;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				width: 220px;
				height: 180px;
			}

			text {
				font-size: 30rpx;
				color: rgba(0, 0, 0, 0.45);
				line-height: 30rpx;
			}
		}

		.order-tab {
			z-index: 99;
			position: fixed;
			width: 100%;
			top: 0;
			background-color: #FFFFFF;
		}

		.list-wrapper {
			padding-top: 108rpx;
		}

		.order-list {
			.order-item {
				background: #fff;
				margin-bottom: 20rpx;

				.order-item-h {
					padding: 28rpx 32rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					align-content: center;
					border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);

					.store-name {
						display: flex;
						flex: 1;
						color: rgba(0, 0, 0, 0.85);
						width: calc(100% - 200rpx);

						.img {
							width: 36rpx;
							height: 36rpx;
							margin-right: 10rpx;
						}

						.name {
							flex: 1;
							max-width: calc(100% - 46rpx);
							word-wrap: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							text-shadow: 0 0 0.1px currentcolor;
							line-clamp: 1;
							-webkit-box-orient: vertical;
						}
					}

					.order-status {
						width: 200rpx;
						text-align: right;
					}
				}

				.order-item-c {
					padding: 16rpx 26rpx;

					.goods-list {
						border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
					}

					.goods-item {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						padding: 20rpx 0;

						.goods-img {
							height: 176rpx;
							width: 176rpx;
							margin-right: 24rpx;

							.img {
								width: 100%;
								height: 100%;
							}
						}

						.goods-info {
							flex: 1;
							display: flex;
							height: 180rpx;
							justify-content: flex-start;
							align-items: center;
							width: calc(100% - 200rpx);
							border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);

							.info {
								width: 100%;
							}

							.goods-name {
								font-size: 15px;
								font-weight: 500;
								color: rgba(0, 0, 0, 0.85);
								margin-bottom: 14rpx;
								word-wrap: break-all;
								overflow: hidden;
								line-break: anywhere;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								line-clamp: 2;
								-webkit-box-orient: vertical;

							}

							.goods-pid {
								margin-bottom: 22rpx;
								color: rgba(0, 0, 0, 0.65);
							}

							.goods-price {
								display: flex;
								width: 100%;
								justify-content: space-between;
								align-items: center;
								margin-top: 48rpx;

								.price {
									font-size: 18px;
									color: #EE3B3B;
									font-weight: bold;
								}

								.num {
									font-size: 14px;
									font-weight: 500;
									color: rgba(0, 0, 0, 0.85);
								}
							}
						}

						.end-goods-info {
							border-bottom: none;
						}
					}
				}

				.goods-total {
					display: flex;
					justify-content: flex-end;
					font-size: 14px;
					color: rgba(0, 0, 0, 0.65);
					padding: 32rpx 26rpx;
					padding-top: 20rpx;

					.total-price {
						color: #F83E48;
						font-weight: 400;
					}
				}

				.order-item-b {
					height: auto;
					overflow: hidden;
					padding-bottom: 20rpx;

					/deep/ .u-button {
						width: auto;
						margin-right: 20rpx;
						float: right;
					}
				}
			}
		}
	}
</style>
